package com.distance;

import java.util.ArrayList;

import com.claim.ClaimsHistoryClient;
import com.settings.LoginClient;
import com.settings.SettingsAdapter;
import com.settings.SettingsParameters;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;


public class ClaimsHistoryList extends Activity{
    ListView claims;
    ArrayAdapter<String> mAdapter;
    private ClaimsHistoryClient claimsHistory;
    private ProgressDialog claimsHistoryProgressDialog;
    private Thread claimsHistoryThread;
    private Thread timerThread;
    private String claimsHistoryString;
    private ClaimsHistoryHandler claimsHistoryHandler;
    public static String[] claimsHistoryStrings;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.claimshistorylist);
	setTitle("Distance - Select Submitted claim to view");
	claims = (ListView)findViewById(R.id.ClaimsHistoryListLV01);
	claimsHistoryHandler = new ClaimsHistoryHandler();
	try{
	    claimsHistory = new ClaimsHistoryClient();
	    claimsHistoryProgressDialog = ProgressDialog.show(ClaimsHistoryList.this, "Please Wait", "Retrieving Claims History", true, false);
	    claimsHistoryThread = new Thread() {
		public void run(){
		    //String addressInput = address.getText().toString(); // Get input text
		    try {
			claimsHistoryString = claimsHistory.claimsHistory();
			//claimsHistoryHandler.sendEmptyMessage(0);
			//loginProgressDialog.dismiss();
		    } catch (Exception e) {
		    }
		}
	    };

	    timerThread = new Thread() {
		public void run(){
		    try {
			Thread.sleep(5000);
			claimsHistoryThread = null;
			claimsHistoryHandler.sendEmptyMessage(0);
			//loginProgressDialog.dismiss();
			//settingsButton.setText(loginString);
		    } catch (Exception e) {
		    }
		}
	    };
	    claimsHistoryThread.start();
	    timerThread.start();
	    this.claims.setOnItemClickListener(new OnItemClickListener(){


		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
			long arg3) {
		    String claimId = claimsHistoryStrings[arg2].split(" ")[0];
		    com.claim.Claim.claimId = claimId;
		    Intent intent = new Intent();
		    intent.setClassName("com.distance", "com.distance.ViewClaim");
		    startActivity(intent);
		    //Toast.makeText(ClaimsHistoryList.this, claimId, Toast.LENGTH_SHORT).show();
		    //Toast.makeText(Start.this, "test "+arg2, Toast.LENGTH_SHORT).show();
		    //Route.start = Bookmarks.locationList.get(arg2);
		    //Bookmarks.detailedLocationPositionToEdit = arg2;
		    //Intent intent = new Intent();
		    //intent.setClassName("com.distance", "com.distance.EditBookmark");
		    //startActivity(intent);
		}

	    });
	    //settingsUsernameEditText.

	}catch(Exception e){

	}



    }


    private class ClaimsHistoryHandler extends Handler{

	@Override
	public void handleMessage(Message msg) {
	    claimsHistoryProgressDialog.dismiss();
	    if(claimsHistoryString==null){
		new AlertDialog.Builder(ClaimsHistoryList.this)
		.setTitle("Login")
		.setMessage("Unable to connect to server.\nPlease try again.")
		.setPositiveButton("OK",null)
		.create()
		.show();
	    }else if(claimsHistoryString.startsWith("Success\n")){
		claimsHistoryString = claimsHistoryString.replace("Success\n", "");
		claimsHistoryStrings = claimsHistoryString.split("\n");
		mAdapter = new ArrayAdapter<String>(ClaimsHistoryList.this, android.R.layout.simple_list_item_1, claimsHistoryStrings);
		claims.setAdapter(mAdapter);
	    }else if(claimsHistoryString.equals("fail")){
		SettingsParameters.loginValidated = false;
		SettingsAdapter.saveSettings();
		new AlertDialog.Builder(ClaimsHistoryList.this)
		.setTitle("Login Error")
		.setMessage("Your username/password combination is wrong.")
		.setPositiveButton("OK",null)
		.create()
		.show();
	    }
	    claimsHistoryString = null;
	    super.handleMessage(msg);
	}

    }

}
